package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 根据用户名，修改用户的密码，昵称，年龄
 * 程序启动后，要求用户顺序输入用户名，密码，昵称，年龄
 * 然后完成需求
 */
public class PWDDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("修改信息");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println(username + "," + password + "," + nickname + "," + age);

        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "UPDATE user " +
                         "SET password='"+password+"',nickname='"+nickname+"',age="+age+" " +
                         "WHERE username='"+username+"'";
            int num = statement.executeUpdate(sql);
            System.out.println(num>0?"修改成功":"修改失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
